// components/order-complete/index.js
import __config from "../../config/env";
const app = getApp()

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        orderId: {
            type: String,
            value: null
        },
    },
    /**
     * 组件的初始数据
     */
    data: {
        loading: false,
        vCodeValue: '',
        isVFocus: false,
        picUrls: [],
        completeDesc: ''
    },
    /**
     * 组件的方法列表
     */
    methods: {
        showVCode: function (e) {
            const that = this;
            that.setData({
                vCodeValue: e.detail.value,
            });
        },
        tapFn(e) {
            const that = this;
            that.setData({
                isVFocus: true,
            });
        },
        ChooseImage() {
            const that = this
            wx.chooseMedia({
                count: 1, //默认9
                sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], //从相册选择
                mediaType: ['image'],
                success: (res) => {
                    let filePath = res.tempFiles[0].tempFilePath
                    wx.showLoading({
                        title: '加载中',
                    })
                    // 上传图片
                    wx.uploadFile({
                        url: __config.basePath + '/mall/api/ma/file/upload',
                        filePath: filePath,
                        header: {
                            'app-id': wx.getAccountInfoSync().miniProgram.appId,
                            'third-session': wx.getStorageSync('third_session') != null ? wx.getStorageSync('third_session') : ''
                        },
                        name: 'file',
                        formData: {
                            'fileType': 'image',
                            'dir': 'order/complete/'
                        },
                        success(uploadFileRes) {
                            if (uploadFileRes.statusCode == '200') {
                                let link = JSON.parse(uploadFileRes.data).link
                                let picUrls = that.data.picUrls
                                if (picUrls) {
                                    picUrls.push(link)
                                } else {
                                    picUrls = []
                                    picUrls.push(link)
                                }
                                that.setData({
                                    picUrls: picUrls
                                })
                            } else {
                                wx.showModal({
                                    title: '提示',
                                    content: '上传失败：' + uploadFileRes.data,
                                    success(res) {
                                    }
                                });
                            }
                        },
                        fail: (err) => {
                            console.log(err)
                        },
                        complete(res) {
                            wx.hideLoading()
                        }
                    })
                }
            });
        },
        ViewImage(e) {
            wx.previewImage({
                urls: this.data.picUrls,
                current: e.currentTarget.dataset.url
            });
        },
        DelImg(e) {
            wx.showModal({
                title: '提示',
                content: '确定删除？',
                cancelText: '取消',
                confirmText: '确定',
                success: res => {
                    if (res.confirm) {
                        this.data.picUrls.splice(e.currentTarget.dataset.index, 1);
                        this.setData({
                            picUrls: this.data.picUrls
                        })
                    }
                }
            })
        },
        // 施工描述
        textareaInput(e) {
            this.setData({
                completeDesc: e.detail.value
            })
        },
        // 提交
        orderSub() {
            const that = this
            let id = this.data.orderId
            let verificationCode = this.data.vCodeValue
            let completeDesc = this.data.completeDesc
            let completePicUrls = this.data.picUrls || []
            let error = ''
            if (!verificationCode) {
                error = '请填写服务码'
            } else if (!completeDesc) {
                error = '请输入施工描述'
            } else if (completePicUrls.length === 0) {
                error = '请上传完工图片'
            }
            if (error) {
                wx.showToast({
                    title: error,
                    icon: "none",
                    duration: 2000
                });
                return
            }
            that.setData({
                loading: true
            })
            app.api.providerOrderReceive({
                id: id,
                verificationCode: verificationCode,
                completeDesc: completeDesc,
                completePicUrls: completePicUrls
            }).then(res => {
                that.setData({
                    loading: false,
                    orderId: null,
                    vCodeValue: null,
                    completeDesc: null,
                    picUrls: []
                })
                that.triggerEvent('completeOrderEvent')
            }).catch(() => {
                this.setData({
                    loading: false
                })
            })
        }
    }
})